package com.github.willisaway.core.util;

import static com.github.willisaway.core.util.ConfigUtil.getProperty;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

public final class HttpUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    /** 重试次数 默认10 */
	public static int RETRY_COUNT = Integer.parseInt(getProperty("retryCount"));
	/** 重试等待时间 默认3s */
	public static int RETRY_DELAY = Integer.parseInt(getProperty("retryDelay"));
	
    private HttpUtil() {
    }

    public static final String httpClientGet(String url) {
        String result = "";
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        try {
            client.executeMethod(getMethod);
            result = getMethod.getResponseBodyAsString();
        } catch (Exception e) {
            logger.error(ExceptionUtil.getStackTraceAsString(e));
        } finally {
            getMethod.releaseConnection();
        }
        return result;
    }

    public static final String httpClientPost(String url, List<NameValuePair> list) {
        String result = "";
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        try {
            NameValuePair[] params = new NameValuePair[list.size()];
            for (int i = 0; i < list.size(); i++) {
                params[i] = list.get(i);
            }
            postMethod.addParameters(params);
            client.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
        } catch (Exception e) {
            logger.error(ExceptionUtil.getStackTraceAsString(e));
        } finally {
            postMethod.releaseConnection();
        }
        return result;
    }
	
	public static String getInputHtmlWithRetry(String url){
		return getInputHtmlWithRetry(url,"UTF-8");
	}
	public static String getInputHtmlWithRetry(String url,String encode){
		String htmlString = null;
		for (int i = 0; i < RETRY_COUNT; i++) {/** 重试次数 默认10 */
			try {
				htmlString = HttpUtil.getInputHtml(url,encode);
				if (i > 0) {
					logger.warn("第" +  (i+1) + "次读取url:" + url);
					Thread.sleep(RETRY_DELAY);// 控制频率
				}
				if (htmlString !=null && htmlString.length() > 0) {
					break;
				}
			} catch (Exception e) {
				logger.error(ExceptionUtil.getStackTraceAsString(e));
			}
		}
		return htmlString;
	}
	
	public static String getInputHtml(String url,Map<String,String> params) {
		String concatedUrl = url;
		//拼接参数
		if(params!=null) {
			for(Map.Entry<String, String> entry : params.entrySet()){
				String mapKey = entry.getKey();
				String mapValue = entry.getValue();
				if(concatedUrl.equalsIgnoreCase(url)) {
					concatedUrl = concatedUrl + "?";
				}else {
					concatedUrl = concatedUrl + "&";
				}
				concatedUrl = concatedUrl + mapKey + "="+mapValue;
			}
		}
		return getInputHtml(concatedUrl,"UTF-8");
	}
	
	public static String getInputHtml(String url) {
		return getInputHtml(url,"UTF-8");
	}
	/**
	 * 读取html内容
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getInputHtml(String url,String encode) {
		//===================================================
		// 拷nodeList = myParser.parse(lastFilter);	
		// 
		// Exception in thread "main" org.htmlparser.util.ParserException: 
		// problem reading a character at position 18965;java.io.EOFException
		//===================================================
		InputStream input = null;
		BufferedReader in = null;
		String newLine = null;
		StringBuffer html = new StringBuffer();
		try {
			input = getInputStream(url);
			if(input!=null){
				in = new BufferedReader(new InputStreamReader(input, encode));
				while ((newLine = in.readLine()) != null) {
					html.append(newLine);
				}
			}
		} catch (Exception ex) {
			html = new StringBuffer();//清空
			ex.printStackTrace();
			logger.info("读取html超时：message=" + ex.getLocalizedMessage() + ", url=" + url);
		} finally {
			if(in != null)
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(input !=null)
				try {
					input.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}

		return html.toString();
	}
	public static InputStream getInputStream(String url) throws Exception{
		URL MyURL = null;
		HttpURLConnection httpCon = null;
		InputStream input = null;
		try {
			MyURL = new URL(url);
			httpCon = (HttpURLConnection)MyURL.openConnection();
			httpCon.setReadTimeout(10000);// 等待10s
			//雪球取数据需要cookie
			if(url.indexOf("xueqiu")>0) {
				httpCon.setRequestProperty("Cookie", CrawlerUtil.getXueqiuCookie());
			}
			//qq
//			if(url.indexOf("qq.")>0) {
//                httpCon.setRequestProperty("Cookie", CrawlerUtil.getQQCookie(url));
//            }
			if(httpCon.getResponseCode() == HttpURLConnection.HTTP_OK){
				input = httpCon.getInputStream();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return input;
	}
}
